<template>
  <div class="container">
    <el-dialog title="新增目录" :visible="panelShow" width="400px" @close="cancel">
      <el-form ref="myform" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="所属学科" prop="subjectID">
          <el-select v-model="form.subjectID" placeholder="请选择" style="width:280px">
            <el-option v-for="(item, index) in simple" :key="index" :label="item.subjectName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="tagName">
          <el-input v-model="form.tagName" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list } from '@/api/hmmm/subjects.js'
import { add } from '@/api/hmmm/tags.js'
export default {
  // 控制面板的显示与隐藏
  props: ['panelShow', 'id'],
  data () {
    return {
      form: {
        subjectID: '',
        tagName: ''
      },
      // 获取学科列表
      simple: [],
      rules: {
        subjectID: [{ required: true, message: '学科不能为空', trigger: 'change' }],
        tagName: [{ required: true, message: '目录名称不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // getData(type, pid) {
    //   this.form.type = type
    //   this.form.pid = pid
    // },
    // async getTagsAdd() {
    //   const res = await getCompanys()
    //   this.form = res.data.data
    // },
    // 取消
    cancel () {
      // 关闭面板
      this.$emit('update:panelShow', false)
      this.form = {
        subjectID: '',
        tagName: ''
      }
    },
    // 提交
    submit () {
      // 全局验证
      this.$refs.myform.validate(async res => {
        if (res) {
          await add(this.form)
          // 关闭面板
          this.$emit('update:panelShow', false)
          // 清空数据
          this.form = {
            subjectID: '',
            tagName: ''
          }
          this.$message.success('添加成功')
          // 重新加载数据
          this.$emit('getListData')
        } else {
          console.log('验证不通过')
        }
      })
    },
    // 渲染数据表单
    async getSubList () {
      const { data: res } = await list()
      // console.log(res)
      this.simple = res.items
    }
  }
}
</script>

<style scoped lang="scss">
.el-dialog {
  width: 80px;
}
</style>
